<?php

class sfAssetThumbPeer extends BasesfAssetThumbPeer
{
    /**
     * Enter description here...
     *
     * @param unknown_type $asset_id
     * @param unknown_type $w
     * @param unknown_type $h
     * @return sfAssetThumb
     */
    public static function get_by_all($asset_id , $w , $h , $create_thumb_image = true){


        if( $asset_id instanceof sfAsset ) {
            $asset    = clone $asset_id;
            $asset_id = $asset->getId();
        } else {
            $asset    = sfAssetPeer::retrieveByPK( $asset_id );
        }

        $c = new Criteria();
        $c->add( self::ASSET_ID , $asset_id );
        $c->add( self::WIDTH    , $w );
        $c->add( self::HEIGHT   , $h );
        $thumb_obj =  self::doSelectOne($c);

        if(!$thumb_obj){

            if($create_thumb_image){

                $files = $asset->getFilepaths();
                $full_size_path = $files['full'];
                list($from_image_w, $from_image_h, $type, $attr) = getimagesize($full_size_path);

                $r = sfAssetToolkit::_create_cropped_image(
                $full_size_path
                , $asset_id.'_'.$asset->getFilename()
                ,$w
                ,$h
                ,0
                ,0
                ,$from_image_w
                ,$from_image_h
                );

                $thumb_obj = new sfAssetThumb();
                $thumb_obj->setWidth($w);
                $thumb_obj->setHeight($h);
                $thumb_obj->setAssetId($asset_id);
                if($r){
                    $thumb_obj->save();
                }


            }


        }




        return $thumb_obj;
    }
}
